var Db = require('../../public/javascripts/Db')
var express = require('express');
var router = express.Router();
var fs = require('fs')
var path = require('path')
var multer = require('multer')
var endMassage = require('../../public/javascripts/endMassage')
//设置保存的路径  dest  
var upload = multer({ dest: "public/images" })
//提交用户多个照片 formdata 里面字段是myfiles的数组
router.post('/postPtoList', upload.array('myfiles', 10), (req, res) => {
    console.log("=========================================");
    console.log("=========================================");
    let fileObj = req.files;
    console.log('fileObj', fileObj);
    let urlList = fileObj.map(i => {
        let oldname = i.path  //获取path: 'public\\upload\\0f625978d5d1a783b12e149718f8b634',
        let newname = i.path + i.originalname //.jpg
        fs.renameSync(oldname, newname)//将老的文件名改成新的有后缀的文件 #同步写法
        return newname.slice(7).replace("\\", "\/");
    })
    res.send(endMassage({ data: "信息插入成功", urlList }))
})
//提交单个文件  formdata 里面字段是myfiles的对象
router.post('/postPto', upload.single('myfiles'), (req, res) => {
    // console.log("=========================================");
    let fileObj = req.file;
    // console.log('fileObj.myfiles', fileObj);
    let oldname = fileObj.path  //获取path: 'public\\upload\\0f625978d5d1a783b12e149718f8b634',
    let newname = fileObj.path + fileObj.originalname //.jpg
    fs.renameSync(oldname, newname)//将老的文件名改成新的有后缀的文件 #同步写法
    let urlList = []
    urlList.push(newname.slice(7).replace("\\", "\/"))
    res.send(endMassage({ data: "信息插入成功", urlList }))
})
//提交用户信息 更改用户信息
router.post('/postInputItem', (req, res) => {
    // console.log("=========================================");
    let { name, sex, tel, uuid, pro, city, area, fileUrl } = req.body
    console.log('req.body', req.body);

    //读取文件内容
    Db.DBFun(`select uuid from user_info where uuid='${uuid}'`, (data) => {
        if (data.length > 0) {
            Db.DBFun(`update  user_info set  name='${name}',sex='${sex}',tel='${tel}',pro='${pro}',city='${city}',area='${area}',fileUrl="${fileUrl}" where uuid='${uuid}'`, (data) => {
                res.send(endMassage({ data: "信息更新成功" }))
            })
        } else {
            Db.DBFun(`insert into user_info(uuid,name,sex,tel,pro,city,area,fileUrl) values ('${uuid}','${name}','${sex}','${tel}','${pro}','${city}','${area}','${fileUrl}')`, (data) => {
                res.send(endMassage({ data: "信息插入成功" }))
            })
        }
    })
})
//queryId  通过id查询 user 用户信息
router.get('/queryId', function (req, res, next) {
    let { uuid } = req.query
    Db.DBFun(`select * from user_info where uuid='${uuid}'`, async (data) => {
        let forFun = async (item) => {

            let prostr = await Db.DBFunPro(`select pro_name from pros where pro_id='${item.pro}'`)
            let citystr = await Db.DBFunPro(`select city_name from cities where city_id='${item.city}'`)
            let areastr = await Db.DBFunPro(`select area_name from areas where area_id='${item.area}'`)
            return prostr[0].pro_name + citystr[0].city_name + areastr[0].area_name
        }
        //for可以加上
        for (let index = 0; index < data.length; index++) {
            data[index].codeStr = ''
            if (data[index].pro == null || data[index].city == null || data[index].area == null) {
                break
            }
            let codeStr = await forFun(data[index])
            data[index].codeStr = codeStr
        }
        res.send(endMassage(data))
    })
});
//所有 user 用户信息
router.get('/queryAllUserInfo', async function (req, res, next) {
    let data = await Db.DBFunPro(`select user_info.uuid,user_info.name,user_info.sex,user_info.tel,user_info.pro,user_info.city,user_info.area,user.username,user.password,user_info.fileUrl from  user_info, user  where user.uuid = user_info.uuid`)
    let forFun = async (item) => {
        let prostr = await Db.DBFunPro(`select pro_name from pros where pro_id='${item.pro}'`)
        let citystr = await Db.DBFunPro(`select city_name from cities where city_id='${item.city}'`)
        let areastr = await Db.DBFunPro(`select area_name from areas where area_id='${item.area}'`)
        return prostr[0].pro_name + citystr[0].city_name + areastr[0].area_name
    }
    //for可以加上
    for (let index = 0; index < data.length; index++) {
        data[index].codeStr = ''
        if (data[index].pro == null || data[index].city == null || data[index].area == null) {
            break
        }
        let codeStr = await forFun(data[index])
        data[index].codeStr = codeStr
    }
    res.send(endMassage(data))
});
//删除单个用户接口
router.get('/delUserInfo', async (req, res) => {
    let { uuid } = req.query
    //uuid 不存在
    if (!uuid) {
        res.send(endMassage({ data: "确实必穿参数uuid", code: 0 }))
        return
    }
    await Db.DBFunPro(`delete from  user where uuid='${uuid}'`)
    await Db.DBFunPro(`delete from  user_info where uuid='${uuid}'`)
    let fileUrl = await Db.DBFunPro(`select fileUrl  from  user_info where uuid='${uuid}'`)
    console.log(fileUrl, 'fileUrlfileUrl');
    // console.log('/public/' + fileUrl[0].fileUrl);
    if (fileUrl.length > 0)
        await fs.unlinkSync('public/' + fileUrl[0].fileUrl)
    res.send(endMassage({ data: "删除用户成功~", code: '1' }))
})




module.exports = router

